
package com.neegix.system.role.interfaces.controller;

import com.neegix.base.PageVO;
import com.neegix.cqrs.command.UniversalCommandBus;
import com.neegix.cqrs.query.UniversalQueryBus;
import com.neegix.inferfaces.result.Result;
import com.neegix.system.role.application.service.command.CreateRoleCommand;
import com.neegix.system.role.application.service.command.DeleteRoleCommand;
import com.neegix.system.role.application.service.command.UpdateRoleCommand;
import com.neegix.system.role.application.service.command.mapper.RoleCommandMapper;
import com.neegix.system.role.application.service.query.GetAllRoleQuery;
import com.neegix.system.role.application.service.query.GetPageRoleQuery;
import com.neegix.system.role.application.service.query.GetRoleDetailQuery;
import com.neegix.system.role.application.service.query.mapper.RoleQueryMapper;
import com.neegix.system.role.interfaces.form.NewRoleForm;
import com.neegix.system.role.interfaces.form.QueryRoleForm;
import com.neegix.system.role.interfaces.form.UpdateRoleForm;
import com.neegix.system.role.interfaces.vo.RolePageVO;
import com.neegix.system.role.interfaces.vo.RoleVO;
import com.neegix.system.role.application.service.command.DisabledCommand;
import com.neegix.system.role.application.service.command.EnabledCommand;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-25 11:32:40
  */

@RestController
@RequestMapping("/api/v1/role")
public class RoleController {

    @Autowired
    private UniversalQueryBus queryBus;

    @Autowired
    private UniversalCommandBus commandBus;

    @PreAuthorize("hasAuthority('system:role:add')")
    @PostMapping
    public Result<Void> createRole(@RequestBody @Valid NewRoleForm roleForm){
        CreateRoleCommand command = RoleCommandMapper.INSTANCE.covertCommand(roleForm);
        return Result.success("创建成功", commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('system:role:modify')")
    @PutMapping("/{id}")
    public Result<Void> updateRole(@PathVariable("id") Long id, @RequestBody @Valid UpdateRoleForm roleForm){

        UpdateRoleCommand command = RoleCommandMapper.INSTANCE.covertCommand(roleForm);
        command.setId(id);
        return Result.success("更新成功",commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result<PageVO<RolePageVO>> getRoles(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @ModelAttribute QueryRoleForm roleForm){
        if(roleForm == null) {
            roleForm = new QueryRoleForm();
        }
        GetPageRoleQuery query = RoleQueryMapper.INSTANCE.covertToGetQuery(roleForm);
        query.setCurrentPage(currentPage);
        query.setPageSize(pageSize);

        PageVO<RolePageVO> pageVO = queryBus.execute(query);
        return Result.success("查询成功",pageVO);
    }


     @PreAuthorize("hasAuthority('system:role:list')")
     @GetMapping
     public Result<List<RoleVO>> getAllRoles(){
         return Result.success("查询成功", queryBus.execute(new GetAllRoleQuery()));
     }

    @PreAuthorize("hasAuthority('system:role:get')")
    @GetMapping("/{id}")
    public Result<RoleVO> getRoleById(@PathVariable("id") Long id) {
        return Result.success("获取成功", queryBus.execute(new GetRoleDetailQuery(id)));
    }

    @PreAuthorize("hasAuthority('system:role:remove')")
    @DeleteMapping
    public Result<Void> removeRole(@RequestBody List<Long> ids){
        DeleteRoleCommand command = new DeleteRoleCommand(ids);
        return Result.success("删除成功", commandBus.execute(command));
    }


     @PutMapping("/{pkRole}/enabled")
     public Result<Void> enabled(@PathVariable("pkRole") Long pkRole){
         commandBus.execute(new EnabledCommand(pkRole));
         return Result.success();
     }

     @PutMapping("/{pkRole}/disabled")
     public Result<Void> disabled(@PathVariable("pkRole") Long pkRole){
         commandBus.execute(new DisabledCommand(pkRole));
         return Result.success();
     }
}